import Request from '@/utils/request';
import qs from 'qs';

const request = new Request({
  baseURL: 'http://localhost:8080',
  timeout: 10000,
});

// 分类相关接口
export const categoryApi = {
  // 获取分类列表
  getList: (params) => request.post('/cms/category/list', params),
  // 获取分类详情
  getDetail: (id) => request.get(`/cms/category/get?id=${id}`),
  // 获取分类下的文章列表
  getArticles: (categoryId, params) => request.post(`/cms/category/${categoryId}/articles`, params),
};

// 文章相关接口
export const articleApi = {
  // 获取文章列表
  getList: (params) => {
    // 处理flag参数，如果有flag参数，转换为后端需要的格式
    const requestParams = { ...params };
    if (requestParams.flag) {
      // 如果是模糊搜索，添加like标记
      if (requestParams.flagLike) {
        requestParams.flagLike = requestParams.flag;
        delete requestParams.flag; // 删除原始flag参数
      }
      console.log('添加flag筛选条件:', requestParams.flag || requestParams.flagLike);
    }
    return request.post('/cms/content/list', requestParams);
  },
  // 获取文章详情
  getDetail: (id) => request.get(`/cms/content/get?id=${id}`),
  // 搜索文章
  search: (params) => request.get('/mcms/search', params),
  // 获取文章点击数
  getHit: (contentId) => request.get(`/cms/content/${contentId}/hit`),
  // 获取推荐文章
  getRecommend: (params) => request.get('/cms/content/recommend', { params }),
  // 获取热门文章
  getHot: (params) => request.get('/cms/content/hot', { params }),
  // 通过标签模糊查询文章接口
  getListByTag: ({ tag, pageNo, size }) => {
    // 手动拼接 query string，确保参数名和后端一致
    const params = [];
    if (tag) params.push(`tag=${encodeURIComponent(tag)}`);
    if (pageNo) params.push(`pageNo=${pageNo}`);
    if (size) params.push(`size=${size}`);
    const query = params.length ? `?${params.join('&')}` : '';
    return request.get(`/cms/content/tag-list${query}`);
  },
};

// 模板相关接口
export const templateApi = {
  // 获取模板列表
  getList: () => request.get('/cms/template/list'),
  // 获取模板详情
  getDetail: (id) => request.get(`/cms/template/get?id=${id}`),
  // 获取页面模板配置
  getPageTemplate: (pageType) => request.get(`/cms/template/page/${pageType}`),
};

// 站点配置接口
export const siteApi = {
  // 获取站点信息
  getInfo: () => request.get('/cms/site/info'),
  // 获取站点配置
  getConfig: () => request.get('/cms/site/config'),
  // 获取导航菜单
  getMenu: () => request.get('/cms/site/menu'),
};

// 分享相关接口
export const shareApi = {
  // 创建分享链接
  create: (params) => request.post(
    '/api/cms/share/create',
    qs.stringify({
      shareType: params.shareType || 'content',
      targetId: params.targetId,
      shareTitle: params.shareTitle,
      shareDescription: params.shareDescription,
      expireDays: params.expireDays || 7
    }),
    {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    }
  ),
  // 查看分享内容
  view: (token) => request.get(`/api/cms/share/view?token=${token}`),
  // 获取分享的栏目信息
  getCategory: (token) => request.get(`/api/cms/share/category?token=${token}`),
  // 获取分享的文章信息
  getContent: (token) => request.get(`/api/cms/share/content?token=${token}`),
  // 获取分享栏目下的文章列表
  getCategoryContents: (token) => request.get(`/api/cms/share/category-contents?token=${token}`),
};